import { Component, OnInit } from '@angular/core';
import { PaginationModel } from "../../../../widgets/pagination/pagination.component";
import { AnnuityPageService } from "./annuity-page.service";
import { LayoutComponent } from './../../../global/layout/layout.component';
import { Router, NavigationEnd, ActivatedRoute } from '@angular/router';
import { environment } from '../../../../../environments/environment';
import { DataService } from '../../../../services/data.service';
import * as _ from 'lodash';

@Component({
  selector: 'app-annuity-page',
  templateUrl: './annuity-page.component.html',
  styleUrls: ['./annuity-page.component.scss'],
  providers: [AnnuityPageService]
})
export class AnnuityPageComponent implements OnInit {

  public menuResourceSet;
  public menuData;
  public dataList;
  public isCheckedAll: any = false;

  // 列表分页配置
  public listPageInfo = {
    currentPageNum: 1,
    pageSize: 10,
    totalPages: 1,
    total: 0,
    pagesShow: 5,
    startRow: 0,
    endRow: 0,
    pageList: [5, 10, 25, 50, 100]
  }
  // 分页列表post数据
  public getBody = {
    pageIndex: 1,
    pageSize: 10,
    sStatus: "fundInfo",
    sFundType: '4',
    sFullName: '',  //组合名称 
    sFundCode: '', //组合代码 
    sLinkPrincipalNo: '' //受托人
  }

  constructor(
    public annuityPageService: AnnuityPageService,
    public router: Router,
    public layoutComponent: LayoutComponent,
    public dataService: DataService
  ) { }

  ngOnInit() {
    this.list();
  }

  // 列表全选
  checkedAll() {
    if (this.isCheckedAll) {
      _.forEach(this.dataList, item => {
        item.isChecked = true;
      });
    } else {
      _.forEach(this.dataList, item => {
        item.isChecked = false;
      });
    }
  }
  // 列表是否全选
  ctrlCheckAll() {
    let that = this;
    let checkList = [];
    _.forEach(this.dataList, item => {
      if (item.isChecked == true) {
        checkList.push(item);
      }
    })
    if (checkList.length == this.dataList.length) {
      this.isCheckedAll = true;
    } else {
      this.isCheckedAll = false;
    }
  }
  list(pageNum?, pageSize?) {
    this.isCheckedAll = false;
    this.getBody.sFullName = _.trim(this.getBody.sFullName);
    this.getBody.pageIndex = pageNum ? pageNum : this.listPageInfo.currentPageNum;
    this.getBody.pageSize = pageSize ? pageSize : this.listPageInfo.pageSize;
    this.annuityPageService.list(this.getBody).subscribe(data => {
      this.dataList = data.list;
      this.listPageInfo.currentPageNum = data.pageNum;
      this.listPageInfo.totalPages = data.pages;
      this.listPageInfo.total = data.total;
      this.listPageInfo.startRow = data.startRow;
      this.listPageInfo.endRow = data.endRow;
    });
  }
  // 分页
  sizeChange(pageSize: number) {
    if (pageSize !== this.listPageInfo.pageSize) {
      this.listPageInfo.pageSize = pageSize;
      this.list();
    }
  }
  // 分页
  pageNavigation(currentPageNum: number) {
    this.listPageInfo.currentPageNum = currentPageNum;
    this.list(this.listPageInfo.currentPageNum, this.listPageInfo.pageSize);
  }
  edit(item) {
    let that = this;
    let sStatusZh: any = '';
    let sStatus: any = '';
    switch (item.sStatus) {
      case 'wyz':
        sStatusZh = '未运作';
        sStatus = 'not-running';
        break;
      case 'yyz':
        sStatusZh = '运作中';
        sStatus = 'running';
        break;
      case 'yzz':
        sStatusZh = '已终止';
        sStatus = 'stop';
        break;
    }
    sessionStorage.setItem("currentTitle", "详情·" + sStatusZh + "·年金");
    that.layoutComponent.nav(null, null, "详情·" + sStatusZh + "·年金");
    that.router.navigate(["/pages/annuity/" + sStatus + "/detail/edit/" + item.nFundNo]);
  }
  exportExcel() {
    let that = this;
    let checkedItem = [];
    let postBody: any;
    _.forEach(this.dataList, item => {
      if (item.isChecked) {
        checkedItem.push(item.nFundNo);
      }
    })
    let converItem = checkedItem.join(",");
    // 请求body
    postBody = {
      "nFundNoList": checkedItem,
      "sFullName": this.getBody.sFullName,
      "sFundCode": this.getBody.sFundCode,
      "sFundType": this.getBody.sFundType,
      "sStatus": this.getBody.sStatus,
      "sLinkPrincipalNo": this.getBody.sLinkPrincipalNo
    }
    let fileName = '企业年金列表-综合查询-';
    // 请求Url
    let postUrl = environment.server + '/pms/v1/pension/basicInfo/export/pensionDeptList?token=' + sessionStorage.getItem('token');
    // 导出
    this.annuityPageService.exportTarget(postUrl, postBody, fileName);
  }
  // 点击修改sStatu
  changeSStatu(sStatu) {
    this.getBody.sStatus = sStatu;
    this.list();
  }
  // 点击表格头排序
  orderData(colName) {
    let listDataAsc = _.orderBy(this.dataList, [colName], ["asc"]);
    if (JSON.stringify(this.dataList) == JSON.stringify(listDataAsc)) {
      listDataAsc = _.orderBy(this.dataList, [colName], ["desc"]);
    }
    this.dataList = listDataAsc;
  }
}
